package com.ctgu.rabbitmq.rabbitmq.queue.reject;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ ClassName RejectConfig
 * @ Author Alex2
 * @ Date 2025/8/15 8:47
 **/
@Configuration
public class RejectConfig {
    // 正常交换机
    public static final String NORMAL_EXCHANGE_REJECT = "normal_exchange_reject";
    public static final String NORMAL_QUEUE_REJECT = "normal_queue_reject";
    public static final String NORMAL_ROUTING_KEY_REJECT = "normal_routing_key_reject";

    // 死信交换机
    public static final String DLX_EXCHANGE_REJECT = "dlx_exchange_reject";
    public static final String DLX_QUEUE_REJECT = "dlx_queue_reject";
    public static final String DLX_ROUTING_KEY_REJECT = "dlx_routing_key_reject";

    @Bean
    public DirectExchange normalExchangeReject() {
        return new DirectExchange(NORMAL_EXCHANGE_REJECT);
    }

    @Bean
    public DirectExchange dlxExchangeReject() {
        return new DirectExchange(DLX_EXCHANGE_REJECT);
    }

    // 绑定死信交换机
    @Bean
    public Queue normalQueueReject() {
        return QueueBuilder.nonDurable(NORMAL_QUEUE_REJECT)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE_REJECT)
                .withArgument("x-dead-letter-routing-key", DLX_ROUTING_KEY_REJECT)
                .build();
    }

    @Bean
    public Queue dlxQueueReject() {
        return new Queue(DLX_QUEUE_REJECT, false);
    }

    @Bean
    public Binding bindingNormalQueueReject() {
        return BindingBuilder.bind(normalQueueReject())
                .to(normalExchangeReject())
                .with(NORMAL_ROUTING_KEY_REJECT);
    }

    @Bean
    public Binding bindingDlxQueueReject() {
        return BindingBuilder.bind(dlxQueueReject())
                .to(dlxExchangeReject())
                .with(DLX_ROUTING_KEY_REJECT);
    }
}